﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using MS.Core.Constants.Objects;
using LHS.Core.TableInfo;
using MS.Core.Base.Transaction;

namespace LHS.Win
{
    public partial class frmLapHoaDonYTa : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmLapHoaDonYTa(string _patID, string _patName, string _clrISN)
        {
            this.patID = _patID;
            this.patName = _patName;
            this.clrISN = _clrISN;
            this.InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btThanhTien_Click(object sender, EventArgs e)
        {
            if (this.check())
            {
                if (!this.txtTienThuThuat.ReadOnly && this.txtTienThuThuat.Value == 0)
                {
                    MessageBox.Show("Vui lòng nhập tiền thủ thuật lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                if (!this.txtTienPhauThuat.ReadOnly && this.txtTienPhauThuat.Value == 0)
                {
                    MessageBox.Show("Vui lòng nhập tiền phẫu thuật lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    if (this.txtTongCong.Value == 0)
                    {
                        this.txtTienKham_Leave(sender, e);
                    }

                    TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                    try
                    {
                        trans.BeginTransaction();
                        DataTable dtInvoice = InfoInvoices.GetStructure();
                        DataRow drInvoice = dtInvoice.NewRow();
                        drInvoice[InfoInvoices.CLRISN] = this.clrISN;
                        drInvoice[InfoInvoices.INVEXAMINATIONPAYMENT] = txtTienKham.Value;
                        drInvoice[InfoInvoices.INVMEDICINEPAYMENT] = txtTienThuoc.Value;
                        drInvoice[InfoInvoices.INVMEDICINESKILLPAYMENT] = txtTienThuThuat.Value;
                        drInvoice[InfoInvoices.INVSURGERYPAYMENT] = txtTienPhauThuat.Value;
                        drInvoice[InfoInvoices.INVTOTALPAYMENT] = txtTongCong.Value;
                        drInvoice[InfoInvoices.INVPAYMENTSTATUS] = 0;
                        dtInvoice.Rows.Add(drInvoice);
                        objController.InsertRow(drInvoice, new string[] { InfoInvoices.INVISN});
                      
                        string isn = Class.isn;
                        object str2 = objController.GetIDENT_CURRENT(InfoInvoices.TABLENAME);

                        //insert vao bang UserUpdatedInvoices 
                        DataTable dtUserUpdatedInvoices = InfoUserUpdatedInvoices.GetStructure();
                        DataRow drUserUpdatedInvoices = dtUserUpdatedInvoices.NewRow();
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USRISN] = isn;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.INVISN] = str2.ToString();
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNDATE] = DateTime.Now;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNREASON] = "Nhập hóa đơn";
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNSTATUS] = 1;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.USNMONEYBEFOREUPDATE] = 0;
                        drUserUpdatedInvoices[InfoUserUpdatedInvoices.CLRISN] = this.clrISN;
                        objController.InsertRow(drUserUpdatedInvoices, new string[] { InfoUserUpdatedInvoices.USNISN });

                        trans.CommitTransaction();
                        MessageBox.Show("Hóa đơn đã được lưu", "Nhập thành công", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        UserInformation.rowUpdated = true;
                    }
                    catch (Exception ex)
                    {
                        trans.RollbackTransaction();
                        Class.ShowException(ex);
                    }
                    this.btThanhTien.Enabled = false;
                    this.txtTienKham.Enabled = false;
                    this.txtTienThuoc.Enabled = false;
                    this.txtTienThuThuat.Enabled = false;
                    this.txtTienPhauThuat.Enabled = false;


                }
            }
        }

        private bool check()
        {
            if (this.txtTienKham.ReadOnly == false && this.txtTienKham.Value == 0)
            {
                MessageBox.Show("Vui lòng nhập tiền khám lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            if (this.txtTienThuoc.ReadOnly == false && this.txtTienThuoc.Value == 0)
            {
                MessageBox.Show("Vui lòng nhập tiền thuốc lớn hơn 0!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
           
            return true;
        }

        private void frmLapHoaDonYTa_Load(object sender, EventArgs e)
        {
            UserInformation.rowUpdated = false;
            this.lbID.Text = this.patID;
            this.lbBenhNhan.Text = this.patName;

            try
            {
                ArrayList arrColumn = new ArrayList();
                arrColumn.Add(InfoClinicalRecord.CLRSKILL);
                arrColumn.Add(InfoClinicalRecord.CLRSURGERY);
                arrColumn.Add("HasPrescription");

                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoClinicalRecord.CLRISN, this.clrISN));
                DataTable dt = objController.GetData("vw_CalculateInvoices", false, arrColumn, param);
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0][0].ToString() == string.Empty)
                    {
                        this.txtTienThuThuat.Value = 0;
                        this.txtTienThuThuat.ReadOnly = true;
                    }
                    if (dt.Rows[0][1].ToString() == string.Empty)
                    {
                        this.txtTienPhauThuat.Value = 0;
                        this.txtTienPhauThuat.ReadOnly = true;
                    }
                    if (dt.Rows[0][2].ToString() == "0")
                    {
                        this.txtTienThuoc.Value = 0;
                        this.txtTienThuoc.ReadOnly = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.txtTienThuThuat.Value = 0;
                this.txtTienThuThuat.ReadOnly = true;
                this.txtTienPhauThuat.Value = 0;
                this.txtTienPhauThuat.ReadOnly = true;
                this.txtTienThuoc.Value = 0;
                this.txtTienThuoc.ReadOnly = true;
                Class.ShowException(ex);
            }
        }

        /// <summary>
        /// tinh lai tong cong khi leave bat cu hang muc tinh tien nao
        /// </summary>
        private void tinhtongcong()
        {
            this.txtTongCong.Value = txtTienPhauThuat.Value + txtTienKham.Value + txtTienThuoc.Value + txtTienThuThuat.Value;
        }

        private void txtTienKham_Leave(object sender, EventArgs e)
        {
            tinhtongcong();
        }
    }
}